package io.itit.ecp.admin.provide.helper;

import cn.hutool.core.date.DateTime;
import io.itit.ecp.admin.common.constants.EmissionFactorConstants;
import io.itit.ecp.admin.common.constants.ParamConstants;
import io.itit.ecp.admin.server.entity.CarbonSinkFactorEntity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/*
 **********************
 * @Author tangdongming
 * @ClassName CarbonSinkFactorHelper
 * @Description
 * @Date 2023/12/1 11:32
 **********************
 **/
@Component
public class CarbonSinkFactorHelper extends CarbonAbsFactorHelper {

    private static CarbonSinkFactorHelper carbonSinkFactorHelper;

    public static CarbonSinkFactorHelper i() {
        return carbonSinkFactorHelper;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        carbonSinkFactorHelper = this;
    }

    @Override
    String getKeyPrefix() {
        return EmissionFactorConstants.CARBON_SINK_FACTOR_KEY;
    }

    @Override
    public BigDecimal getFactor(String type, DateTime dateTime) {
        CarbonSinkFactorEntity entity = carbonSinkFactorService
                .lambdaQuery()
                .le(CarbonSinkFactorEntity::getSolidifyDate, dateTime)
                .eq(CarbonSinkFactorEntity::getType, type)
                .last(ParamConstants.LIMIT_ONE)
                .one();
        return entity == null ? null : entity.getFactor();
    }

}
